import pycurl
from io import BytesIO
import json
import os


def get_token(url, user, passwd):
    c = pycurl.Curl()
    b = BytesIO()
    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.setopt(pycurl.USERPWD, '%s:%s' % (user, passwd))
    c.setopt(pycurl.HTTPHEADER, ["Content-Type: application/json"])
    c.setopt(pycurl.URL, (url + '/token/').encode('utf-8'))
    c.perform()

    result = json.loads(b.getvalue())
    errcode = result['errcode']

    code = c.getinfo(pycurl.HTTP_CODE)
    b.close()
    c.close()
    if code == 200 and errcode == 0:
        return result['token']
    else:
        return (code, b.getvalue())


class StsAdmin:
    def __init__(self, url, user, passwd):
        self.__url = url
        self.__user = user
        self.__passwd = passwd

    def insert_records(self, table_name, records):
        c = pycurl.Curl()
        b = BytesIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.USERPWD, '%s:%s' % (self.__user, self.__passwd))
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.HTTPHEADER,
                 ["Content-Type: application/json;charset=utf-8"])
        c.setopt(
            pycurl.URL,
            os.path.join(self.__url, 'record', table_name).replace('\\', '/').encode('utf-8'))
        data = json.dumps(records).encode('utf-8')
        c.setopt(pycurl.POSTFIELDS, data)
        c.perform()

        result = json.loads(b.getvalue())
        errcode = result['errcode']
        code = c.getinfo(pycurl.HTTP_CODE)
        b.close()
        c.close()
        if code == 200 and errcode == 0:
            return 0
        else:
            return code

    def insert_records_by_schema(self, schema_name, table_name, records):
        c = pycurl.Curl()
        b = BytesIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.USERPWD, '%s:%s' % (self.__user, self.__passwd))
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.HTTPHEADER,
                 ["Content-Type: application/json;charset=utf-8"])
        c.setopt(
            pycurl.URL,
            os.path.join(self.__url, 'record', schema_name,
                         table_name).replace('\\', '/').encode('utf-8'))
        data = json.dumps(records).encode('utf-8')
        c.setopt(pycurl.POSTFIELDS, data)
        c.perform()

        result = json.loads(b.getvalue())
        errcode = result['errcode']
        code = c.getinfo(pycurl.HTTP_CODE)
        b.close()
        c.close()
        if code == 200 and errcode == 0:
            return 0
        else:
            return code


class StsAdminByToken:
    def __init__(self, url, token):
        self.__url = url
        self.__token = token

    def insert_records(self, table_name, records):
        c = pycurl.Curl()
        b = BytesIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.HTTPHEADER, [
            "Content-Type: application/json;charset=utf-8", "token: {}".format(
                self.__token)
        ])
        c.setopt(
            pycurl.URL,
            os.path.join(self.__url, 'record', table_name).replace('\\', '/').encode('utf-8'))
        data = json.dumps(records).encode('utf-8')
        c.setopt(pycurl.POSTFIELDS, data)
        c.perform()

        result = json.loads(b.getvalue())
        errcode = result['errcode']
        code = c.getinfo(pycurl.HTTP_CODE)
        b.close()
        c.close()
        if code == 200 and errcode == 0:
            return 0
        else:
            return (code, b.getvalue())

    def insert_records_by_schema(self, schema_name, table_name, records):
        c = pycurl.Curl()
        b = BytesIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.HTTPHEADER, [
            "Content-Type: application/json;charset=utf-8", "token: {}".format(
                self.__token)
        ])
        c.setopt(
            pycurl.URL,
            os.path.join(self.__url, 'record', schema_name,
                         table_name).replace('\\', '/').encode('utf-8'))
        data = json.dumps(records).encode('utf-8')
        c.setopt(pycurl.POSTFIELDS, data)
        c.perform()

        result = json.loads(b.getvalue())
        errcode = result['errcode']
        code = c.getinfo(pycurl.HTTP_CODE)
        b.close()
        c.close()
        if code == 200 and errcode == 0:
            return 0
        else:
            return (code, b.getvalue())
